package com.lhd.charging.order.common;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * json 和 对象转换工具
 */
public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();


    /**
     * 对象转换为json
     * @param object
     * @return
     */
    public static String toJson(Object object){

        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json转对象
     * @param json
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> T toObject(String json,Class<T> clazz){

        try {
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
